package com.example.gameapplication.plant;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.Log;

import com.example.gameapplication.BaseObject;
import com.example.gameapplication.activity.MainActivity;
import com.example.gameapplication.bullet.Bullet;
import com.example.gameapplication.R;
import com.example.gameapplication.bullet.DoubleBullet;
import com.example.gameapplication.database.DataUtils;

import java.util.ArrayList;

public class MainPlane extends BaseObject {
    public int speed;//移动速度
    public float leftx;//该对象的左上角x
    public float lefty;//该对象的左上角y
    public float rightx;//该对象的右下角x
    public float righty;//该对象的右下角y
    public int object_width;//该对象的宽度
    public int object_height;//该对象的高度
    private Bitmap mPlane;
    private Bitmap mPlaneExplosion;
    public int currentFrame; // 战机等级，因为图片有三种战机，所以指定为3个等级
    public float screen_width;    // 屏幕宽度
    public float screen_height;  // 屏幕高度
    public boolean isOnPress = false;
    public Bitmap plane_shield;
    public ArrayList<Bullet> bullets;
    public int shieldWidth;
    public int shieldHeight;
    public int flasTime = 30;//死亡画面更换的次数
    public int unbeatable = 100;//无敌画面的次数

    public MainPlane(Resources resources, Paint paint) {
        super(resources, paint);
        bullets = new ArrayList<>();
    }

    public ArrayList<Bullet> getBullets() {
        return bullets;
    }


    public void setBullets(ArrayList<Bullet> bullets) {
        this.bullets = bullets;
    }

    @Override
    public void initBitmap() {
        mPlane = BitmapFactory.decodeResource(resources, R.drawable.myplane);
        mPlaneExplosion = BitmapFactory.decodeResource(resources,
                R.drawable.myplaneexplosion);
        object_width = mPlane.getWidth() / 3;//这个图片包含飞机的3个状态
        object_height = mPlane.getHeight();
        plane_shield = BitmapFactory.decodeResource(resources, R.drawable.plane_shield);
        shieldWidth = plane_shield.getWidth();
        shieldHeight = plane_shield.getHeight();
    }

    @Override
    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public void setScreenWH(float screen_width, float screen_height) {
        this.screen_width = screen_width;
        this.screen_height = screen_height;
        leftx = screen_width / 2 - object_width / 2;
        lefty = screen_height - object_height;
        midx = leftx + object_width / 2;
        midy = lefty + object_height / 2;
        rightx = leftx + object_width;
        righty = midy + object_height / 2;
    }

    /**
     * 绘制机体
     *
     * @param canvas
     */
    @Override
    public void drawSelf(Canvas canvas) {
        boolean setBulletAlife = false;//没一次只能复活一个子弹，防止子弹叠在一起
        //画子弹
        for (int i = 0; i < bullets.size(); i++) {
            Bullet bullet = bullets.get(i);
            if (!bullet.isAlive() && isAlive()) {
                if (!setBulletAlife) {//每次回复子1个，防止子弹重叠到一起
                    bullet.setSeat(midx, midy);
                    bullet.setAlive(true);
                    bullet.setLv(getLv());
                    MainActivity.palySound(1, 0);
                    setBulletAlife = true;
                }
            }
            bullet.drawSelf(canvas);
        }
        if (isAlive()) {
            drawPlant(canvas);
        } else {
            if (flasTime > 0) {

                flasTime--;
                drawExplosion(canvas);
            } else {
                setAlive(true);
                unbeatable = 100;
                flasTime = 30;
            }
        }
        addBullet();
    }

    public void addBullet() {
        if (bullets.size() == DataUtils.getMyPlaneBulletSize()) return;//节省运行资源
        while (bullets.size() > DataUtils.getMyPlaneBulletSize()) {
            bullets.get(0).dataDestroyed();
            bullets.remove(0);
        }
        if (bullets.size() == DataUtils.getMyPlaneBulletSize()) return;
        Bullet e = new DoubleBullet(resources, paint);
        e.setSeat(midx, midy);
        e.setSpeed(80);
        bullets.add(e);
        MainActivity.palySound(1, 0);
    }

    public void checkOnclick(float x, float y) {
        if (Math.abs(x - midx) < object_width / 2 && Math.abs(y - midy) < object_height / 2) {
            isOnPress = true&&DataUtils.isIsPlaying();
        }
    }

    public boolean isOnPress() {
        return isOnPress;
    }

    public void setOnPress(boolean onPress) {
        isOnPress = onPress;
    }

    //更新该对象的位置，传入中心点的坐标
    public void setSeat(float x, float y) {
        if (!isOnPress || !isAlive()) return;
        midx = x;
        midy = y;
        leftx = midx - object_width / 2;
        lefty = midy - object_height / 2;
        rightx = leftx + object_width;
        righty = lefty + object_height;
    }

    /**
     * 绘制爆炸时的机体
     *
     * @param canvas
     */
    private void drawExplosion(Canvas canvas) {
        int x = (int) (currentFrame * object_width);
        canvas.save();
        canvas.clipRect(leftx, lefty, rightx,
                righty);
        canvas.drawBitmap(mPlaneExplosion, leftx - x, lefty, paint);
        canvas.restore();
        if (currentFrame < 5) {
            currentFrame++;
        } else {
            currentFrame = 0;
        }
    }

    //画普通机体
    public void drawPlant(Canvas canvas) {
        canvas.save();
        //画护甲
        if (unbeatable > 0) {
            unbeatable--;
            canvas.drawBitmap(plane_shield, midx - shieldWidth / 2, midy - shieldHeight / 2, paint);
        }
        canvas.clipRect(leftx, lefty, rightx, righty);
        if (mPlane == null) {//实际运行中没进入过这个
            initBitmap();
        } else if (mPlane.isRecycled()) {//后台切换的时候可能被回收，实际运行中遇到过
            initBitmap();
        }
        canvas.drawBitmap(mPlane, getLv() < 3 ? leftx - (getLv() - 1) * object_width : leftx - 2 * object_width, lefty, paint);//大于3只按照第三级的画，只有三种状态
        canvas.restore();
    }

    @Override
    public void setAlive(boolean alive) {
        super.setAlive(alive);
        if (!alive) {
            if (DataUtils.setMylife(DataUtils.getMylife() - 1) <= 0) {
                DataUtils.setIsPlaying(false);
                DataUtils.playButtonClick();
                MainActivity.showGameOverDialog();
            }
        }
    }

    @Override
    public void dataDestroyed() {
        for (int i = 0; i < bullets.size(); i++) {
            Bullet bullet = bullets.get(i);
            bullet.dataDestroyed();
        }
        if (!mPlane.isRecycled()) {
            mPlane.recycle();
        }
        if (!mPlaneExplosion.isRecycled()) {
            mPlaneExplosion.recycle();
        }
    }
}
